package com.timer;

import com.javatest.Test;
import net.sf.json.util.JSONUtils;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author LL
 * @create 2021-01-08 15:43
 */
public class Timer1 {

    public static void test01(){
        final long timerInterval = 1000;
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                int a = 0;
                while (true){
                    if (a == 10){
                        //要运行的任务代码
                        System.out.println("结束了！");
                        return;
                    }
                    System.out.println("你好");
                    try {
                        //同步延迟数据，并且会阻塞线程
                        Thread.sleep(timerInterval);
                        a++;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        //创建定时器
        Thread thread = new Thread(runnable);
        thread.start();
    }

    public static void test02(){

        class test extends TimerTask{
            private Timer timer;

            public test(Timer timer) {
                this.timer = timer;
            }

            int a = 0;
            @Override
            public void run() {
                System.out.println("程序执行中");
                if (++a == 3) {
                    this.timer.cancel();//停止执行
                    System.out.println("程序结束");
                }
            }
        }

        Timer timer = new Timer();
        //定义开始等待时间
        long delay = 1000;
        //定义每次执行的间隔时间
        long inPeriod = 1000;
        //安排任务在一段时间内执行
         timer.scheduleAtFixedRate(new test(timer),delay,inPeriod);
    }

    public static void test03(){
        class test implements Runnable {
            private Timer timer;

            public test(Timer timer) {
                this.timer = timer;
            }

            int a = 0;
            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()){
                    System.out.println(a++);
                    System.out.println("程序执行中");
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println(Thread.currentThread().isInterrupted());
                        //Thread.currentThread().interrupt();
                    }
                }
                System.out.println("程序结束");
            }
        }
        Timer timer = new Timer();
        //并发工具类被引进的
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        //第二个参数为首次执行的延迟时间，第三个为参数为定时执行的间隔时间
        //第一次执行的人时间为10秒，然后每隔5秒执行一次
        ScheduledFuture<?> scheduledFuture = service.scheduleAtFixedRate(new test(timer), 5, 1, TimeUnit.SECONDS);
        System.out.println(scheduledFuture);
    }

    public static void main(String[] args) {
        test03();
        double sqrt = Math.sqrt(9);
        System.out.println("sqrt:"+sqrt);
    }
}
